<?php

class Portfolio_Controller_Default extends MVC_AbstractController
{
	private $model;
	
	public function __construct()
	{
		
		parent::__construct();
		
		$this->model = new Portfolio_Model_Default();
	} 

	
	public function portfolioList()
	{ 
		
		$str = sprintf("SELECT DISTINCT p.*, pi.image_name, pt.type_permalink
							FROM portfolio p
							INNER JOIN portfolio_images pi ON pi.portfolio_id = p.id
							INNER JOIN portfolio_type pt ON pt.id = p.type_id
							GROUP BY pi.portfolio_id
							ORDER BY post_date DESC");
		//$row = $this->mysql->query($query)->fetch_assoc();
		//echo $str;
		$result = $this->model->db->query($str);
		
		$dataSet = array();
		$count = 1;
		while($row = $result->fetch())
		{
			$data = '<li class="item" data-id="id-'.$count.'" data-type="'.$row['type_permalink'].'">'.PHP_EOL;
			if(!empty($row['mobile_site'])) $data .= '<div class="mobile"></div>'.PHP_EOL;
			$data .= 	'<div>'.trim($row['title']).'</div>'.PHP_EOL;
			$data .= 	'<a href="/portfolio/'.$row['id'].'/'.$row['permalink'].'">';
			$data .= 		'<img src="http://fatgalah.sb.loc/portfolioImages/tn_'.$row['image_name'].'" alt="" />';
			$data .= 	'</a>'.PHP_EOL;
			$data .= '</li>'.PHP_EOL;
			$dataSet[] = $data;	
			$count++;
		}
		
		$html = $this->getPortfolioType();	
	
		
		$html .= '<div style="margin-left:-38px; width:980px;">'.PHP_EOL;
		$html .= 	'<ul class="portfolio group">'.PHP_EOL;
		$html .= 		implode($dataSet);
		$html .= 	'</ul>'.PHP_EOL;
		$html .= '</div>'.PHP_EOL;
		//var_dump($row);
		//if(!empty($row))
		
		//$html = $column == 'txt' ? CommonUtils::ss($row['txt']) : CommonUtils::ss($row['image']);
		
		return $html;
	}
	
	public function portfolioDetail()
	{
		return $this->uriSegments[1];
	}
	
	public function getPortfolioDetail()
	{
		$id = $this->uriSegments[1];
		//$this->headMetaTags = '<meta getPortfolioDetails '.$id.'/>';
		
		$str = sprintf("SELECT p.*, pt.type_name
							FROM portfolio p
							INNER JOIN portfolio_type pt ON pt.id=p.type_id
							WHERE p.id='%s'",
							$id);
		//echo $str;
		
		$result = $this->model->db->query($str);
		$row = $result->fetch();
		foreach($row as $k => $v)
		{
			$$k = stripslashes($v);
		}
		$html = '<div class="portfolioImageWrapper">';
		$html .= 	$this->getPortfolioImages($id);
		$html .= '</div>';
		$html .= '<div class="portfolioInfo">';
		$html .= 	'<h1>'.$title.'</h1>';
		
			if(!empty($mobile_site)) $mobile_site = '<li><a href="http://'.$mobile_site.'">Visit Mobile</a></li>';
			
			$html .= 	'<div class="filterPreview">';
			$html .= 		'<ul>';
			$html .= 			'<li><a href="http://'.$row['url'].'">Visit Website</a></li>';
			$html .= 			$mobile_site;
			$html .= 		'</ul>';
			$html .= 	'</div>';
			//$html .= 	'<div class="clear"></div>';
			
			$html .= '<div class="portfolioPreview">';
			if(!empty($row['type_name']))
			{
				$html .= 	'<p><span>Project Type:</span>'.$row['type_name'].' website design</p>';
				$html .= 	'<br class="clear"/>';
			}
			if(!empty($row['techonology']))
			{
				$html .= 	'<p><span>Techonology:</span>'.$row['techonology'].'</p>';
				$html .= 	'<br class="clear"/>';
			}
			if(!empty($row['api']))
			{
				$html .= 	'<p><span>API applications:</span>'.$row['api'].'</p>';
				$html .= 	'<br class="clear"/>';
			}
			
			$html .= 	'<p><span>Modules:</span>'.$row['modules'].'</p>';
			$html .= 	'<br class="clear"/>';
			
			$html .= 	'<p><span>Description:</span>'.$row['txt'].'</p>';
			$html .= 	'<br class="clear"/>';
		
			$html .= '</div>';
		
		//$html .= '<div class="clear"></div>';
		/*
		$html .= '<div class="portfolioDesciption">';
		$html .= 	'<span>Description:</span>';
		$html .= 		$row['txt'];*/
		$html .= '</div>';
		$html .= '<div class="clear"></div>';
		return $html;
	}

	protected function getPortfolioType()
	{
		
		$opt = '';
		
		$str = sprintf("SELECT DISTINCT pt.*
							FROM portfolio_type pt
							INNER JOIN portfolio p ON p.type_id = pt.id
							ORDER BY type_name");
		//echo $str;
		$result = $this->model->db->query($str);
		while($row = $result->fetch())
		{
			
			$opt .= '<li class="'.$row['type_permalink'].'"><a href="#">'.$row['type_name'].'</a></li>'.PHP_EOL;
		}
		$html = '<dl class="group">';
		$html .= 	'<dt>Filter:</dt>';
		$html .= 		'<dd>';
		$html .= 			'<ul class="filter group">'; 
		$html .= 				'<li class="current all"><a href="#">All</a></li>'; 
		$html .= 				$opt; 
		$html .= 			'</ul> ';
		$html .= 		'</dd>';
		$html .= '</dl>';
		return $html;	
	}
	protected function getPortfolioImages($id)
	{
		$str = sprintf("SELECT pi.*
							FROM portfolio_images pi
							WHERE pi.portfolio_id='%s'",
							$id);
		//echo $str; exit;
		$result = $this->model->db->query($str);
		
		
		while($row = $result->fetch())
		{
			switch($row['type_site'])
			{
				case 'M':
					$mob = '<div>';
					$mob .= 	'<img src="http://fatgalah.sb.loc/portfolioImages/originals/'.$row['image_name'].'" alt="" />';
					$mob .= '</div>';
				break;
				default:
				case 'W':
					$web = '<div>';
					$web .= 	'<img class="portfolioImg" src="http://fatgalah.sb.loc/portfolioImages/originals/'.$row['image_name'].'" alt="" />';
					$web .= 	'<div class="dropShadow510"></div>';
					$web .= '</div>';
				break;
			}
		}
		return $web.$mob;
	}
	public static function recentPortfolio()
	{
		$query = sprintf("SELECT p.*, pi.image_name, pi.type_site
							FROM portfolio p
							INNER JOIN portfolio_images pi ON pi.portfolio_id=p.id
							WHERE p.hightlighted = 'Y'
								AND p.posted = 'Y'
								AND pi.type_site='W'
							GROUP BY pi.portfolio_id
							ORDER BY p.post_date DESC");
							
		var_dump(Config::get('mysql')->query($query));
		$dataSet = array();
		/*
		$result = $this->model->db->query($str);
		
		while($row = $result->fetch())
		{
			$title = Util::ss(strtoupper($row['title']));
			$data = '<li>';
			$data .= 	'<div>';
			$data .= 		$title;
			$data .= 	'</div>';
			$data .= 	'<a href="'.URL.'/portfolio/'.$row['id'].'/'.$row['permalink'].'" title="'.$title.'">';
			$data .= 		'<img src="http://fatgalah.sb.loc/portfolioImages/tn_'.$row['image_name'].'" alt="'.$title.'" />';
			$data .= 	'</a>';
			$data .= '</li>';
			$dataSet[] = $data;
		
		}*/
		$html = '<div class="recent_project_crousal">';
        $html .= 	'<h2 class="our_rectent_clients_heading">Our Recent Clients</h2>';
        $html .= 		'<div class="crousal_wrapper">';
        $html .= 			'<ul class="crousal">';
		$html .= 			implode('', $dataSet);	
        $html .= 			'</ul>';
        $html .= 		'</div>';
        $html .= 	'<span class="prev">prev</span>';
        $html .= 	'<span class="next">next</span>';
        $html .= '</div>';
		if(Util::isHomepage()) return $html;
	}

}
